package gemini.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.Shape;
import javax.swing.JPanel;

public class GPanel extends JPanel {
    private Image tileimage;
    private int tilewidth = 0;
    private int tileheight = 0;
    private Rectangle rb;
    private Insets ri;
    
    /**
     * Create a JTiledPanel with the given image.
     * The tile argument may be null, you can set it later
     * with setTileImage().  Note that a JTiledPanel is
     * always opaque.
     * 
     * @param tile 
     */
    public GPanel(Image tile) {
        super();
        setTileImage(tile);
        setOpaque(true);
	rb = new Rectangle(0,0,1,1);
	ri = new Insets(0,0,0,0);
    }

    /**
     * Create a JTiledPanel with the given image and 
     * layout manager and double buffering status.  
     * Either or both of the first two arguments
     * may be null.
     * 
     * @param tile 
     * @param mgr 
     * @param isDB 
     */
    public GPanel(Image tile, LayoutManager mgr, boolean isDB)
    {
        super(mgr, isDB);
        setTileImage(tile);
        setOpaque(true);
	rb = new Rectangle(0,0,1,1);
	ri = new Insets(0,0,0,0);
    }

    /**
     * Get the current tiling image, or null if there
     * isn't any right now.
     * 
     * @return Image
     */
    public Image getTileImage() { 
        return tileimage; 
    }

    /**
     * Set the current tiling image.  To prevent tiling,
     * call this method with null.  Note that this method
     * does NOT call repaint for you; if you want the panel
     * to repaint immediately, you must call repaint() 
     * yourself.
     * 
     * @param tile 
     */
    private void setTileImage(Image tile) {
        tileimage = tile;
        tilewidth = 0;
        tileheight = 0;
    }

    

    /**
     * Paint this component, including the tiled
     * background image, if any.
     * 
     * @param g
     */
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (tileimage != null && tilewidth <= 0) {
            tileheight = tileimage.getHeight(this);
            tilewidth = tileimage.getWidth(this);
        }
        if (tileimage != null && tilewidth > 0) {
            Color bg = getBackground();
            getBounds(rb);
            Insets riv = getInsets(ri);
            rb.translate(riv.left, riv.top);
            rb.width -= (riv.left + riv.right);
            rb.height -= (riv.top + riv.bottom);
            Shape ccache = g.getClip();
            g.clipRect(rb.x, rb.y, rb.width, rb.height);
            int xp, yp;
            for(yp = rb.y; yp < rb.y + rb.height; yp += tileheight) {
                for(xp = rb.x; xp < rb.x + rb.width; xp += tilewidth) {
                    g.drawImage(tileimage, xp, yp, bg, this);
                }
            }
            g.setClip(ccache);
        }
    }
    
}